package fileUtils

import (
	"io/fs"
	"path"
	"path/filepath"
	"strings"
)

//type DirEntry = fs.DirEntry

type FileInfo struct {
	Path  string
	Name  string
	IsDir bool
	Info  fs.FileInfo
}

func newFileFileInfo(path, name string, isDir bool, info fs.FileInfo) *FileInfo {
	return &FileInfo{path, name, isDir, info}
}
func WalkFile(rootPath string) []*FileInfo {
	var infoTree []*FileInfo
	filepath.Walk(rootPath, func(path string, info fs.FileInfo, err error) error {
		infoTree = append(infoTree, newFileFileInfo(path, info.Name(), info.IsDir(), info))
		if err != nil {
			return err
		}
		return nil
	})
	return infoTree

}
func WalkFileDir(rootPath string) []*FileInfo {
	var pathsTree []*FileInfo
	filepath.WalkDir(rootPath, func(path string, d fs.DirEntry, err error) error {
		info, err1 := d.Info()
		if err1 != nil {
			return err1
		}
		pathsTree = append(pathsTree,
			newFileFileInfo(path, d.Name(), d.IsDir(), info))
		if err != nil {
			return err
		}
		return nil
	})
	return pathsTree

}
func GetFileName(fullPath string) (fullFilename, fileNameOnly, fileSuffix string) {
	fullFilename = fullPath
	//获取文件名带后缀
	filenameWithSuffix := path.Base(fullFilename)
	//获取文件后缀
	fileSuffix = path.Ext(filenameWithSuffix)
	//获取文件名
	fileNameOnly = strings.TrimSuffix(filenameWithSuffix, fileSuffix)
	return
}
